<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
    <jsp:include page="../inc/js.jsp"/>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>排放量详情管理</title>
    <jsp:include page="../inc/css.jsp"/>
    <script src="static/js/echarts.min.js"></script>
</head>
<body class="hold-transition sidebar-mini">
<div class="wrapper">
    <jsp:include page="../inc/menu.jsp"/>

    <!-- Content Wrapper. Contains page content -->
    <div class="content-wrapper">
        <!-- Main content -->
        <!-- Main content -->
        <section class="content">
            <div class="row">
                <div class="col-12">
                    <div class="card" style="margin-top: 10px">
                        <!-- /.card-header -->
                        <div class="card-header">
                            <div class="form-inline" style="margin-top: 10px;">
                                <button type="button" title="查询" class="btn btn-info" style="margin-right: 10px;" onclick="_companyBalanceInfoList.fn.reloadData()">查询</button>
                                <button type="button" title="返回公司列表" class="btn btn-warning" onclick="_companyBalanceInfoList.fn.goBack()">返回</button>
                            </div>
                        </div>
                        <div class="card-body">
                            <div class="table-responsive">
                                <table class="table table-striped table-bordered table-hover" id="dataTables" style="width: 99%">
                                    <colgroup>
                                        <col class="gradeA even"/>
                                        <col class="gradeA odd"/>
                                        <col class="gradeA even"/>
                                        <col class="gradeA odd"/>
                                    </colgroup>
                                    <thead>
                                    <tr>
                                        <th>污染源因子</th>
                                        <th>设备名称</th>
                                        <th>是否汇总</th>
                                        <th style="width: 120px">操作</th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                        <!-- /.card-body -->
                    </div>
                    <!-- /.card -->
                </div>
                <!-- /.col -->
            </div>
            <!-- /.row -->
        </section>
        <!-- /.content -->
    </div>
</div>
<!-- ./wrapper -->

<input type="hidden" id="hiddenCompanyBalanceId" value="${companyBalanceId}"/>

<!-- page script -->
<script type="text/javascript">
    let _companyBalanceInfoList = {
        v: {
            list: [],
            dTable: null
        },
        fn: {
            init: function () {
                _companyBalanceInfoList.fn.dataTableInit();
            },
            reloadData: function () {
                _companyBalanceInfoList.v.list = [];
                _companyBalanceInfoList.v.dTable.DataTable().ajax.reload(null, false);
            },
            goBack: function () {
                window.history.go(-1);
            },
            dataTableInit: function () {
                _companyBalanceInfoList.v.dTable = $tutu.dataTable($('#dataTables'), {
                    "processing": true,
                    "serverSide": true,
                    "autoWidth": true,//自动计算宽度
                    "lengthChange": false,//是否允许用户改变表格每页显示的记录数
                    "searching": false,
                    "ordering": false,
                    "ajax": {
                        "url": "backend/balance/child/list",
                        "type": "POST"
                    },
                    "language": $tutu.v.dataTableL,
                    "columns": [
                        {"data": "companyBalance.codeDictName"},
                        {
                            "data": "device",
                            render: function (data) {
                                if (null != data && data !== '') {
                                    if (data.name !== '') {
                                        return data.name;
                                    } else {
                                        return '';
                                    }
                                } else {
                                    return "";
                                }
                            }
                        },
                        {
                            "data": "status",
                            render: function (data) {
                                if (data === 1) {
                                    return '<span style="color: green;">是</span>';
                                } else {
                                    return '<span style="color: red;">否</span>';
                                }
                            }
                        },
                        {"data": null}
                    ],
                    /*添加小插件*/
                    "columnDefs": [
                        {
                            "data": null,
                            "defaultContent": "<button type='button' title='添加汇总' class='btn btn-primary btn-sm edit'><i class='fa fa-recycle'></i></button>",
                            "targets": -1
                        }
                    ],
                    /*展示数据格式化*/
                    "createdRow": function (row, data, index) {
                        _companyBalanceInfoList.v.list.push(data);
                    },
                    /*回调函数*/
                    rowCallback: function (row, data) {
                        if (data.status === 1) {
                            // 此时为已添加汇总
                            $('td', row).last().find(".edit").prop('title', '取消汇总').removeClass('btn-primary').addClass('btn-danger');
                        }else{
                            // 此时为已取消汇总
                            $('td', row).last().find(".edit").prop('title', '添加汇总').removeClass('btn-danger').addClass('btn-primary');
                        }

                        $('td', row).last().find(".edit").click(function () {
                            _companyBalanceInfoList.fn.editInfo(data);
                        });
                    },
                    /*带参数*/
                    "fnServerParams": function (aoData) {
                        _companyBalanceInfoList.v.list = [];

                        aoData.companyBalanceId = $('#hiddenCompanyBalanceId').val();
                    },
                    "fnDrawCallback": function (data) {
                        $tutu.uiForm(data);
                    }
                });
            },
            editInfo: function (data) {
                startLoading();

                $.post('backend/balance/child/save', {
                    id: data.id,
                    status: data.status
                }, function (result) {
                    endLoading();

                    if (result.code === 0) {
                        info("信息已保存", "success");

                        _companyBalanceInfoList.fn.reloadData();
                    } else {
                        info(result.msg, "error");
                    }
                });
            }
        }
    };

    $(document).ready(function () {
        _companyBalanceInfoList.fn.init();

        window.onload = function () {
            checkSelectMenu("balanceLi");

            loadMenuColor('balanceLi');
        }
    });
</script>
</body>
</html>